#!/bin/bash

NUMBEROFTEST=0
NUMBEROFSUCCESS=0

# Note LW:
# - heritageparam3 is excluded because it cannot work: at serialization dynamic type of serialized object cannot be different to static type
EXCLUDEDTEST="runtests\|runtests_short\|CMake\|xmp1\|xmp2\|xmp3\|xmp4\|xmp-raw\|static1\|heritageparam3"

SXXLOCATION=/usr/local/popc/sbin
TEST_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
RESULTS=$TEST_DIR/tests_results
CWD=$( pwd )

## Note: Test flags can be passed to all tests via command line :
## e.g.: TEST_FLAGS=-pseudo-dynamic ./runtest -all
if [ "x${TEST_FLAGS:-}" != "x" ]
then
    # override with the value given by external variable
    TEST_FLAGS="FLAGS=${TEST_FLAGS:-}"
fi

if [ "x" != "x$POPC_LOCATION" ]
then
SXXLOCATION=$POPC_LOCATION/sbin/
fi

if [ "$1" = "-all" ]
then
    $SXXLOCATION/SXXpopc killnstart

    echo ""
    echo Starting test suite...
    echo ""
    rm -rf $RESULTS
    for test in `ls -F -d $TEST_DIR/*/ | grep -v $EXCLUDEDTEST`
    do
        test_name=$( basename $test )
        echo "---------------------------------------------------------------------------"
        echo "*** $test_name test ($NUMBEROFTEST) **"
        echo "---------------------------------------------------------------------------"
        cd $test
        ((NUMBEROFTEST++))
        make clean
        make        $TEST_FLAGS
        make run
        if [ "$?" = "0" ]
        then
            echo "$test succeed" >> $RESULTS
            ((NUMBEROFSUCCESS++))
        else
            echo "$test failed" >> $RESULTS
        fi
        make clean
        cd $CWD
        echo ""
    done
    echo "---------------------------------------------------------------------------"
    echo "*** TEST SUITE COMPLETED: RESULTS ARE SHOWN BELOW ***"
    echo "---------------------------------------------------------------------------"
    cat $RESULTS
    echo "$NUMBEROFSUCCESS succeed on $NUMBEROFTEST"
    if [ "$NUMBEROFSUCCESS" = "$NUMBEROFTEST" ]
    then
        echo "ALL TESTS SUCCEED! You are ready to use POP-C++!"
        echo "Warning: Take caution with test results, read test output for additional information"
    fi
    rm -rf $RESULTS

    $SXXLOCATION/SXXpopc stop 2&> /dev/null
    $SXXLOCATION/SXXpopc kill 2&> /dev/null
else
    if [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ -z "$1" ]
    then
        echo ""
        echo "usage : ./runtests <parameter>"
        echo ""
        echo "with parameter :"
        echo ""
        echo "-h  or  --help  : displays this help message"
        echo "-all            : launches all tests at once"
        echo ""
        echo "    one of the following test name :"
        echo ""
        echo "barrier, callback, classparam, constparam, exception, heritage, heritageparam1,"
        echo "heritageparam2, jobManager, matrixNB, method, param, passparam, serialize_vect,"
        echo "structparam, templateparam, tree, vertorint1, vectorint2, vectorx"
        echo ""
        echo "                : will launch the specific test"
        echo ""
        exit
    else
        selected_test=$TEST_DIR/$1

        if [ -d $selected_test ]
        then
            $SXXLOCATION/SXXpopc killnstart

            cd $selected_test
            echo ""
            echo "Start of \"$1\" test"
            echo ""
            make clean
	    make        $TEST_FLAGS
            echo "---------------------------------------------------------------------------"
            echo "*** $1 test ***"
            echo "---------------------------------------------------------------------------"
            make run
            make clean
            cd $CWD
            echo ""
            echo "End of \"$1\" test"

            $SXXLOCATION/SXXpopc stop 2&> /dev/null
            $SXXLOCATION/SXXpopc kill 2&> /dev/null
        else
            echo ""
            echo "The test \"$1\" does not exist !!"
            echo ""
            $0 -h
        fi
    fi
fi
